//
//  MeetingVideoViewController.m
//  YUNWUMeetingDemo
//
//  Created by mac on 2019/7/11.
//  Copyright © 2019 DGL. All rights reserved.
//

#import "MeetingVideoViewController.h"
#import "SDKUtil.h"

#import "MeetingHelper.h"
#import "MChatModel.h"
#import "MeetingChatView.h"
#import "DGLCustomPopViewController.h"
#import "MeetingSelectViewController.h"
#import "MeetingBottomView.h"
#import "MeetingContentView.h"

@interface MeetingVideoViewController ()<UINavigationControllerDelegate,UIViewControllerTransitioningDelegate,CloudroomVideoMeetingCallBack, CloudroomVideoMgrCallBack, BKBrushViewDelegate>
{
    UILabel *_roomLabel;
}
@property (nonatomic, strong) MeetingContentView *contentView;//视频view
@property (nonatomic, strong) MeetingBottomView *bottomView;//底部按钮
@property (nonatomic, strong) MeetingSelectViewController *meetingSelectView;//分辨率、帧率
@property (nonatomic, strong) CLShareView *shareView; /**< 屏幕共享 */
@property (nonatomic, strong) MeetingChatView *chatView; /**< IM */

@property (nonatomic, strong) NSMutableArray<UsrVideoId *> *members; /**< 房间成员 */
@property (nonatomic, copy) NSArray<NSString *> *ratioArr; /**< 分辨率集合 */
@property (nonatomic, copy) NSArray<UsrVideoInfo *> *cameraArray; /**< 摄像头集合 */
@property (nonatomic, strong) NSMutableArray<MChatModel *> *messages;
@property (nonatomic, assign) NSInteger curCameraIndex; /**< 当前摄像头索引 */
@property (nonatomic, strong) UIAlertController *dropVC; /**< 掉线弹框 */
@property (nonatomic, assign) BOOL enableMark; /**< 标注? */
@property (nonatomic, assign) int devID;


@end

@implementation MeetingVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"meetInfo = %@",_meetInfo);
    [self _commonSetup];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // 不灭屏
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    
    // FIXME:屏幕共享结束回调接收不到 added by king 20170905
    [self _updateDelegate];
}
#pragma mark - VideoMgrDelegate
// 掉线通知
- (void)lineOff:(CRVIDEOSDK_ERR_DEF)sdkErr {
    if (_dropVC) {
        [_dropVC dismissViewControllerAnimated:NO completion:^{
            _dropVC = nil;
            
            if (sdkErr == CRVIDEOSDK_USER_BEEN_KICKOUT) { // 被踢
                [self _showAlert:@"您已被踢下线!"];
            } else { // 掉线
                [self _showAlert:@"您已掉线!"];
            }
        }];
        return;
    }
    
    if (sdkErr == CRVIDEOSDK_USER_BEEN_KICKOUT) { // 被踢
        [self _showAlert:@"您已被踢下线!"];
    } else { // 掉线
        [self _showAlert:@"您已掉线!"];
    }
}

#pragma mark - VideoMeetingDelegate
// 入会结果
- (void)enterMeetingRslt:(CRVIDEOSDK_ERR_DEF)code {
    [HUDUtil hudHiddenProgress:YES];
    
    if (code == CRVIDEOSDK_NOERR) {
        [self _enterMeetingSuccess];
    } else if (code == CRVIDEOSDK_MEETROOMLOCKED) {// FIXME:房间加锁后,进入房间提示语不正确 added by king 201711291513
        [self _enterMeetingFail:@"房间已加锁!"];
    } else {
        [self _enterMeetingFail:@"进入房间失败!"];
    }
}

- (void)userEnterMeeting:(NSString *)userID {
    NSString *text = [NSString stringWithFormat:@"%@进入房间!", userID];
    [HUDUtil hudShow:text delay:3 animated:YES];
    
    for (MemberInfo *member in [[CloudroomVideoMeeting shareInstance] getAllMembers]) {
        MLog(@"userId:%@ nickName:%@", member.userId, member.nickName);
    }
    
    [self _updateVideoInfo];
}

- (void)userLeftMeeting:(NSString *)userID {
    NSString *text = [NSString stringWithFormat:@"%@离开房间!", userID];
    [HUDUtil hudShow:text delay:3 animated:YES];
    
    [self _updateVideoInfo];
}
// 房间被结束
- (void)meetingStopped{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:@"房间已结束!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self _jumpToPMeeting];
    }];
    [alertController addAction:doneAction];
    [self presentViewController:alertController animated:YES completion:^{}];
}

// 房间掉线
- (void)meetingDropped{
    [self _showReEnter:@"房间掉线!"];
}
// 成员有新的视频图像数据到来(通过GetVideoImg获取
- (void)notifyVideoData:(UsrVideoId *)userID frameTime:(long)frameTime {
    
}
// 本地音频设备有变化
- (void)audioDevChanged{
    [self _updateMic];
}
// 音频设备状态变化
- (void)audioStatusChanged:(NSString *)userID oldStatus:(AUDIO_STATUS)oldStatus newStatus:(AUDIO_STATUS)newStatus {
    [self _updateMic];
}
- (void)micEnergyUpdate:(NSString *)userID oldLevel:(int)oldLevel newLevel:(int)newLevel{
        MLog(@"userID=:%@ oldLevel:%d newLevel:%d", userID, oldLevel, newLevel);
}

// 视频设备状态变化
- (void)videoStatusChanged:(NSString *)userID oldStatus:(VIDEO_STATUS)oldStatus newStatus:(VIDEO_STATUS)newStatus {
    [self _setCamera];
    [self _updateCamera];
    
    // 订阅摄像头
    [self _updateVideoInfo];
}
// 本地视频设备有变化
- (void)videoDevChanged:(NSString *)userID {
    [self _setCamera];
    [self _updateCamera];
    
    // 订阅摄像头
    [self _updateVideoInfo];
}
// 屏幕共享操作通知
- (void)notifyScreenShareStarted{
    [HUDUtil hudShow:@"屏幕共享已开始" delay:3 animated:YES];
    
    _shareView.alpha = 1.0;
}

- (void)notifyScreenShareStopped{
    [HUDUtil hudShow:@"屏幕共享已结束" delay:3 animated:YES];
    
    _shareView.alpha = 0;
}

// 屏幕共享数据更新,用户收到该回调消息后应该调用getShareScreenDecodeImg获取最新的共享数据
- (void)notifyScreenShareData:(NSString *)userID changedRect:(CGRect)changedRect frameSize:(CGSize)size {
    
}

// 视频墙分屏模式回调(0:互看 1:1分屏 2:2分屏 3:4分屏 4:5分屏 5:6分屏 6:9分屏 7:13分屏 8:16分屏)
- (void)notifyVideoWallMode:(int)wallMode {
}
// IM 消息发送结果
- (void)sendIMmsgRlst:(NSString *)taskID sdkErr:(CRVIDEOSDK_ERR_DEF)sdkErr cookie:(NSString *)cookie {
    if (sdkErr != CRVIDEOSDK_NOERR) {
        [HUDUtil hudShow:@"消息发送失败" delay:3 animated:YES];
    }
}
// 通知收到 IM 消息
- (void)notifyIMmsg:(NSString *)romUserID text:(NSString *)text sendTime:(int)sendTime {
    if (![_chatView isShow]) {
        [_chatView showAnimation];
    }
    
    MChatModel *chatModel = [[MChatModel alloc] initWithName:romUserID content:text];
    [self.messages addObject:chatModel];
    _chatView.message = [self.messages copy];
}
// 屏幕共享标注开始回调
- (void)notifyScreenMarkStarted {
    
}

// 屏幕共享标注停止回调
- (void)notifyScreenMarkStopped{
}

// 屏幕共享是否允许他人标注回调
- (void)enableOtherMark:(BOOL)enable {
    
}

// 屏幕共享标注回调
- (void)sendMarkData:(MarkData *)markData {
    
}
// 屏幕共享所有标注回调
- (void)sendAllMarkData:(NSArray<MarkData *> *)markDatas {
    
}

// 清除所有屏幕共享标注回调
- (void)clearAllMarks{
}

- (void)setNickNameRsp:(CRVIDEOSDK_ERR_DEF)sdkErr userid:(NSString*)userid newName:(NSString*)newName
{
}

- (void)notifyNickNameChanged:(NSString*)userid oldName:(NSString*)oldName newName:(NSString*)newName
{
}
#pragma mark - private method
- (void)_commonSetup {
    _roomLabel = [[UILabel alloc]init];
    _roomLabel.text = [NSString stringWithFormat:@"房间号:%d",_meetInfo.ID];
    _roomLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_roomLabel];
    [_roomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.mas_equalTo(self.view);
        if (IsiPhoneX) {
            make.height.mas_equalTo(88);
        }else{
            make.height.mas_equalTo(50);
        }
    }];
    
    _contentView = [[MeetingContentView alloc]init];
    _contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_contentView];
//    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(self.view);
//        make.top.mas_equalTo(_roomLabel.mas_bottom).offset(10);
//        make.height.mas_equalTo(BOUNDS.size.height/3+20);
//    }];
    // 设置属性
    [self _setupProperty];
    // 更新代理
    [self _updateDelegate];
    // 入会操作
    [self _enterMeeting];
    
    self.shareView.alpha = 0;
    
}
/**
 设置属性
 */
- (void)_setupProperty {
    /* 聊天 */
    MeetingChatView *chatView = [[MeetingChatView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:chatView];
    [self.view insertSubview:chatView belowSubview:_bottomView];
    _chatView = chatView;
    [_chatView setTextFieldShouldReturn:^(MeetingChatView *chatView, NSString *text) {
        NSLog(@"send message:%@", text);
        // 发送 IM 消息
        CloudroomVideoMeeting *cloudroomVideoMeeting = [CloudroomVideoMeeting shareInstance];
        NSString *cookie = [NSString stringWithFormat:@"%f", CFAbsoluteTimeGetCurrent()];
        [cloudroomVideoMeeting sendIMmsg:text toUserID:nil cookie:cookie];
    }];
//    [_chatView setTitle:[NSString stringWithFormat:@"房间号:%d",_meetInfo.ID]];
    _curCameraIndex = 1;
    
    _bottomView = [[MeetingBottomView alloc]init];
    _bottomView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(BOUNDS.size.height/3);
    }];
    
    weakify(self);
    [_bottomView setResponse:^(MeetingBottomView *view, UIButton *sender) {
        strongify(wSelf);
        switch (sender.tag) {
            case MBottomViewBtnTypeMic: {
                [sSelf _handleMic:sender];
                break;
            }
            case MBottomViewBtnTypeCamera: {
                [sSelf _handleCamera:sender];
                break;
            }
            case MBottomViewBtnTypeChat: {
                
                [sSelf _handleChat];
                break;
            }
            case MBottomViewBtnTypeExCamera: {
                [sSelf _handleExCamera];
                break;
            }
            case MBottomViewBtnTypeRatio: {
                [sSelf _handleRatio];
                break;
            }
            case MBottomViewBtnTypeExit: {
                [sSelf _handleExit];
                break;
            }
            case MBottomViewBtnTypeFrame: {
                [wSelf _handleFrame];
                break;
            }
        }
    }];
    
}
- (void)_setStatusBarBG {
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = UIColorFromRGBA(0, 0, 0, 0.5);
    }
}

#pragma mark - UIViewControllerTransitioningDelegate
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    return [[DGLCustomPopViewController
             alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

/**
 初始化摄像头信息
 */
- (void)_setupForCamera {
    CloudroomVideoMeeting *cloudroomVideoMeeting = [CloudroomVideoMeeting shareInstance];
    NSString *myUserID = [cloudroomVideoMeeting getMyUserID];
    short curVideoID = [cloudroomVideoMeeting getDefaultVideo:myUserID];
    NSMutableArray <UsrVideoInfo *> *videoes = [cloudroomVideoMeeting getAllVideoInfo:myUserID];
    NSArray<UsrVideoInfo *> *cameraArray = [videoes copy];
    
    for (UsrVideoInfo *video in videoes) {
        MLog(@"userId:%@ videoName:%@ videoID:%d", video.userId, video.videoName, video.videoID);
        
        if (curVideoID == 0) { // 没有默认设备
            curVideoID = video.videoID;
            [cloudroomVideoMeeting setDefaultVideo:myUserID videoID:curVideoID];
        }
    }
    
    if ([cameraArray count] <= 0) {
        MLog(@"获取摄像头设备为空!");
        return;
    }
    
    _cameraArray = cameraArray;
}
/**
 设置摄像头
 */
- (void)_setCamera {
    if ([_cameraArray checkEmptyOrNil]) {
        MLog(@"没有摄像头!");
        return;
    }
    
    if (_curCameraIndex >= [_cameraArray count]) {
        MLog(@"摄像头索引越界!");
        return;
    }
    
    // 设置摄像头设备
    UsrVideoInfo *video = [_cameraArray objectAtIndex:_curCameraIndex];
    [[CloudroomVideoMeeting shareInstance] setDefaultVideo:video.userId videoID:video.videoID];
    MLog(@"当前摄像头为:%zd", _curCameraIndex);
}
/* 入会操作 */
- (void)_enterMeeting {
    if (_meetInfo.ID > 0) {
        [HUDUtil hudShowProgress:@"正在进入房间..." animated:YES];
        CloudroomVideoMeeting *cloudroomVideoMeeting = [CloudroomVideoMeeting shareInstance];
        NSString *nickname = [[MeetingHelper shareInstance] nickname];
        [cloudroomVideoMeeting enterMeeting:_meetInfo.ID pswd:_meetInfo.pswd userID:nickname nikeName:nickname];
    }
}
/* 入会成功 */
- (void)_enterMeetingSuccess {
    // 打开本地麦克风
    [SDKUtil openLocalMic];
    // 打开本地摄像头
    [SDKUtil openLocalCamera];
    
    [self _updateMic];
    
    [self _setupForCamera];
    
    // 订阅摄像头
    [self _setCamera];
    [self _updateCamera];
    [self _updateVideoInfo];
    
    // 设置默认分辨率: 360*360
    [SDKUtil setRatio:VSIZE_SZ_720];
    // 设置默认帧率: 15
    [SDKUtil setFps:24];
    // 设置默认优先级: 画质优先
    [SDKUtil setPriority:25 min:22];
    // 设置默认宽高比
    [SDKUtil setLocalCameraWHRate:WHRATE_1_1];
}
/**
 入会失败
 @param message 失败信息
 */
- (void)_enterMeetingFail:(NSString *)message {
    [HUDUtil hudShow:message delay:3 animated:YES];
    
    if (_meetInfo.ID > 0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self _jumpToPMeeting];
        }];
        UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self _enterMeeting];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:doneAction];
        [self presentViewController:alertController animated:NO completion:nil];
    }
}
/**
 重登操作
 @param message 重登信息
 */
- (void)_showReEnter:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"离开房间" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self _jumpToPMeeting];
        _dropVC = nil;
    }];
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"重新登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        // 离开房间
        [[CloudroomVideoMeeting shareInstance] exitMeeting];
        // 重新入会
        [self _enterMeeting];
        _dropVC = nil;
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:doneAction];
    [self presentViewController:alertController animated:NO completion:^{}];
    _dropVC = alertController;
}
/* 掉线/被踢下线 */
- (void)_showAlert:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self _jumpToPMeeting];
    }];
    [alertController addAction:doneAction];
    [self presentViewController:alertController animated:YES completion:^{}];
}
// TODO: 更新操作
/* 更新代理 */
- (void)_updateDelegate {
    //    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //    [appDelegate.videoMgrCallBack setVideoMgrDelegate:self];
    //    [appDelegate.videoMeetingCallBack setVideoMeetingDelegate:self];
    CloudroomVideoMgr *cloudroomVideoMgr = [CloudroomVideoMgr shareInstance];
    [cloudroomVideoMgr setMgrCallback:self];
    CloudroomVideoMeeting *cloudroomVideoMeeting = [CloudroomVideoMeeting shareInstance];
    [cloudroomVideoMeeting setMeetingCallBack:self];
}
/* 更新摄像头开关 UI */
- (void)_updateCamera {
    VIDEO_STATUS status = [SDKUtil getLocalCameraStatus];
    [_bottomView updateCamera:!(status == AOPEN || status == AOPENING)];
}
/* 更新麦克风开关 UI */
- (void)_updateMic {
    AUDIO_STATUS status = [SDKUtil getLocalMicStatus];
    [_bottomView updateMic:!(status == VOPEN || status == VOPENING)];
}
/* 更新房间成员 */
- (void)_updateVideoInfo {
    
    CloudroomVideoMeeting *cloudroomVideoMeeting = [CloudroomVideoMeeting shareInstance];
    NSString *myUserID = [cloudroomVideoMeeting getMyUserID];
    NSMutableArray <UsrVideoId *> *watchableVideos = [cloudroomVideoMeeting getWatchableVideos];
    
    
    NSMutableArray <UsrVideoId *> *diff = [NSMutableArray array];
    NSMutableArray <UsrVideoId *> *remove = [NSMutableArray array];
    
    // 找到不同元素
    if ([self.members count] <= 0) {
        [diff addObjectsFromArray:watchableVideos];
    }
    else
    {
        for (UsrVideoId *obj1 in watchableVideos)
        {
            BOOL find = NO;
            for (UsrVideoId *obj2 in self.members)
            {
                if ([obj1.userId isEqualToString:obj2.userId] && obj1.videoID == obj2.videoID)
                {
                    find = YES;
                    break;
                }
            }
            
            if (find == NO)
            {
                [diff addObject:obj1];
            }
        }
        
        // 找到删除元素
        for (UsrVideoId *obj1 in self.members)
        {
            BOOL find = NO;
            for (UsrVideoId *obj2 in watchableVideos)
            {
                if ([obj1.userId isEqualToString:obj2.userId] && obj1.videoID == obj2.videoID)
                {
                    find = YES;
                    break;
                }
            }
            
            if (find == NO)
            {
                [remove addObject:obj1];
            }
        }
        
        for (UsrVideoId *usrVideoId in remove) {
            [self.members removeObject:usrVideoId];
        }
    }
    
    [self.members addObjectsFromArray:diff];
    
    [_contentView updateUIViews:self.members localer:myUserID];
    if (self.members.count >=2) {
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.view);
            make.top.mas_equalTo(_roomLabel.mas_bottom).offset(10);
            make.height.mas_equalTo(BOUNDS.size.height/3);
        }];
    }
    else if (self.members.count < 2) {
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.view);
            make.top.mas_equalTo(_roomLabel.mas_bottom).offset(10);
            make.height.mas_equalTo(BOUNDS.size.height/3+60);
        }];
    }else if (self.members.count <5){
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.view);
            make.top.mas_equalTo(_roomLabel.mas_bottom).offset(10);
            make.height.mas_equalTo(BOUNDS.size.height/3+80);
        }];
    }
}
// TODO: 按钮响应事件
/* 开/关麦克风 */
- (void)_handleMic:(UIButton *)sender {
    if (sender.selected) {
        [SDKUtil closeLocalMic];
        
    } else {
        [SDKUtil openLocalMic];
    }
}
- (void)_handleChat {
    [_chatView clickShow];
}
/* 开/关摄像头 */
- (void)_handleCamera:(UIButton *)sender {
    if (sender.selected) {
        [SDKUtil closeLocalCamera];
    } else {
        [SDKUtil openLocalCamera];
    }
}
-(void)modifyUserName:(NSString*)name
{
    CloudroomVideoMeeting *cloudroomVideoMeeting = [CloudroomVideoMeeting shareInstance];
    NSString *userID = [cloudroomVideoMeeting getMyUserID];
    [cloudroomVideoMeeting setNickName:userID nickName:name];
}

-(void)setupMicMute
{
    CloudroomVideoMeeting *cloudroomVideoMeeting = [CloudroomVideoMeeting shareInstance];
    [cloudroomVideoMeeting setSpeakerMute:true];
}
/* 切换摄像头 */
- (void)_handleExCamera {
    
    // [[CloudroomVideoMeeting shareInstance] setSpeakerMute:true];
    
    
    // FIXME: 切换摄像头之前,先检测是否关闭 (king 20180717)
    if (![SDKUtil isLocalCameraOpen]) {
        [HUDUtil hudShow:@"摄像头已关闭" delay:3 animated:YES];
        return;
    }
    
    if ([_cameraArray count] <= 1) {
        MLog(@"无法切换摄像头!");
        return;
    }
    
    if (_curCameraIndex == 0) {
        _curCameraIndex = 1;
    } else {
        _curCameraIndex = 0;
    }
    
    [self _setCamera];
}
/* 分辨率 */
- (void)_handleRatio {
    /* 分辨率 */
    MeetingSelectViewController *heightPick = [[MeetingSelectViewController alloc]init];
    heightPick.dataSource = @[@"360*360", @"480*480", @"720*720"];
    heightPick.curFrame = [SDKUtil getStringFromRatio];
    heightPick.modalPresentationStyle = UIModalPresentationCustom;
    heightPick.transitioningDelegate = self;
    [self.view endEditing:YES];
    [self presentViewController:heightPick animated:YES completion:^{
    }];
    heightPick.MeetingSelectViewBalck = ^(NSString * _Nonnull selectData) {
        NSLog(@"selectData = %@",selectData);
        [SDKUtil setRatio:[SDKUtil getRatioFromString:selectData]];
    };
    
}
/* 退出 */
- (void)_handleExit {
    // FIXME: 退出房间,防止误操作 (king 20180717)
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"离开房间?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
    UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self _jumpToPMeeting];
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:done];
    [self presentViewController:alertVC animated:YES completion:nil];
}
- (void)_handleFrame {
    /* 帧率 */
    MeetingSelectViewController *heightPick = [[MeetingSelectViewController alloc]init];
    heightPick.dataSource = @[@"10", @"15", @"24"];
    heightPick.curFrame = [SDKUtil getStringFromFrame];
    heightPick.modalPresentationStyle = UIModalPresentationCustom;
    heightPick.transitioningDelegate = self;
    [self.view endEditing:YES];
    [self presentViewController:heightPick animated:YES completion:^{
    }];
    heightPick.MeetingSelectViewBalck = ^(NSString * _Nonnull selectData) {
        NSLog(@"selectData = %@",selectData);
        [SDKUtil setFps:[selectData intValue]];
    };
    
}

/* 跳转到"预入会"界面 */
- (void)_jumpToPMeeting {
    // 离开房间
    [[CloudroomVideoMeeting shareInstance] exitMeeting];
    // 注销
    [[CloudroomVideoMgr shareInstance] logout];
    // 退出界面
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - getter & setter
- (NSMutableArray<UsrVideoId *> *)members {
    if (!_members) {
        _members = [NSMutableArray array];
    }
    
    return _members;
}

- (NSMutableArray<MChatModel *> *)messages {
    if (!_messages) {
        _messages = [NSMutableArray array];
    }
    
    return _messages;
}
#pragma mark - override
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    
    if (![touch.view isEqual:_bottomView]) {
        if (_chatView.isShow) {
            [_chatView hideAnimation];
        } else {
            [_chatView showAnimation];
        }
    }
    
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


@end
